<?php
namespace Admin\Controller;
use Think\Controller;

class StudentController extends \Org\Util\AdminController {
    /**
     * CRUD前后置操作
     */
    public function interfaceBeforeCreate() {
    }
    public function interfaceAfterImport($id) {
        $year = date("Y");
        $len = strlen($id);
        $lenNew = 4-$len;
        $number = $year;
        for ($i=0;$i<$lenNew;$i++) {
            $number .= '0';
        }
        $number .= $id;
        $students=D('Student')->find($id);
        unset($data);
        $data['number']=$number;
        D('student')->where(['id'=>$id])->data($data)->save();
        //找到传过来的学生
        $student=D('Student')->where(['id'=>$id])->find();
        $class=D('class')->where(['id'=>$student['class_id']])->find();
        $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
        $major=D('major')->where(['id'=>$class['major_id']])->find();
        $tuition = D('tuition')->select();
        foreach($tuition as $k=>$v){
            if($grade['id']==$v['grade_id']&&$major['id']==$v['major_id']){
                unset($data);
                $data['student_id']=$student['id'];
                $data['tuition_id']=$v['id'];
                D('fee')->add($data);
            }
        }
    }
    public function interfaceAfterCreate($id) {
        $year = date("Y");
        $len = strlen($id);
        $lenNew = 4-$len;
        $number = $year;
        for ($i=0;$i<$lenNew;$i++) {
            $number .= '0';
        }
        $number .= $id;
        $students=D('Student')->find($id);
        unset($data);
        $data['number']=$number;
        D('student')->where(['id'=>$id])->data($data)->save();
        //找到传过来的学生
        $student=D('Student')->where(['id'=>$id])->find();
        $class=D('class')->where(['id'=>$student['class_id']])->find();
        $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
        $major=D('major')->where(['id'=>$class['major_id']])->find();
        $tuition = D('tuition')->select();
        foreach($tuition as $k=>$v){
            if($grade['id']==$v['grade_id']&&$major['id']==$v['major_id']){
                unset($data);
                $data['student_id']=$student['id'];
                $data['tuition_id']=$v['id'];
                D('fee')->add($data);
            }
        }
        $this->success('新增成功!', U('Student/list'));
        
   }
    public function interfaceBeforeDelete($id) {
    }
    public function interfaceAfterDelete($id) {
        D('fee')->where(['student_id'=>$id])->delete();
        D('Record')->where(['student_id'=>$id])->delete();
    }
    public function interfaceBeforeUpdate($id) {
        if (IS_POST) {
            $student = M('Student')->find($id);
            if ($student['class_id'] != $_POST['class_id']) {
                $_SESSION['needUpdateStudent'.$id] = true;
            } else {
                $_SESSION['needUpdateStudent'.$id] = false;
            }
        }
    }
    public function interfaceAfterUpdate($id) {
        if ($_SESSION['needUpdateStudent'.$id]) {
            D('fee')->where(['student_id'=>$id])->delete();
            $student=D('Student')->where(['id'=>$id])->find();
            $class=D('class')->where(['id'=>$student['class_id']])->find();
            $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
            $major=D('major')->where(['id'=>$class['major_id']])->find();
            $tuition = D('tuition')->select();
            foreach($tuition as $k=>$v){
                if($grade['id']==$v['grade_id']&&$major['id']==$v['major_id']){
                    unset($data);
                    $data['student_id']=$student['id'];
                    $data['tuition_id']=$v['id'];
                    D('fee')->add($data);
                }
            }
            $_SESSION['needUpdateStudent'.$id] = false;
        }
    }
    /**
     * list页面按钮接口
     * return $html
     */
    public function tableOpeationButton() {
    	return "";
    }
    public function wapTableOpeationButton() {
    	return "";
    }
    
    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        parent::create();
    }

    public function delete() {
        parent::delete();
    }

    public function update($id=0) {
        parent::update($id);
    }


    public function list() {
        parent::list();
    }

    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }
}
